<?php
namespace backend\controllers;

use common\components\BaseController;
use SebastianBergmann\CodeCoverage\Report\Xml\Project;
use Yii;
use yii\db\Query;
use \common\helps\tools;
use \common\helps\code;

/**
 * User controller
 */
class MovieController extends BaseController
{

    public $modelClass = 'backend\models\MovieCollege';

    /*
     * 影视学堂列表
     * @param Request $request
     * @author cpy(1442435897@qq.com)
     * @return Json
     */
    public function actionMovieList()
    {
        $model = new \backend\models\MovieCollege();
        $request =$this->request;
        $params = $request->queryParams;

        $total = $model::selectData($params);
        $res = tools::getPageData($total,empty($params['page_size'])?'':$params['page_size'],empty($params['page'])?'':$params['page']);
        if(!empty($params['excel_download']) && $params['excel_download']>0){
            $tableHeader = ['id','标题','简介','排序','发布人','创建时间'];
            $data = [];
            foreach ($total as $k => $v){
                $data[$k]['id'] = $v['id'];
                $data[$k]['title'] = $v['title'];
                $data[$k]['describe'] = $v['describe'];
                $data[$k]['sort'] = $v['sort'];
                $data[$k]['admin_id'] = $v['admin_name'].'/'.$v['admin_id'];
                $data[$k]['created_at'] = $v['created_at'];
            }
            tools::ToPHPEXECL($tableHeader,$data,'影视学堂列表');
        }
        return code::success($res);
    }

    /*
     * 添加
     * @param Request $request
     * @author cpy(1442435897@qq.com)
     * @return Json
     */
    public function actionAddMovie()
    {
        $model = new \backend\models\MovieCollege();
        $session = Yii::$app->session;
        $request =$this->request;
        $params = $request->bodyParams;
        if(!empty($params)) {
            if ($model->load($params, '')) {
                if ($model->validate()) {
                    $params['admin_id'] = $session->get('user_id');
                    $params['admin_name'] = $session->get('username');
//                    if(empty($params['admin_id']) || empty($params['admin_name'])){
//                        return code::error('error','请登录！');
//                    }
                    $res = $model->saveInfo('create',$params);
                    if(empty($res->errorInfo)){
                        return code::success('');
                    }else{
                        return code::error('error');
                    }
                }else{
                    $error = array_values($model->getErrors());
                    return code::error('',$error[0][0]);
                }
            }
        }else{
            $data['adminInfo'] = $admin->findAllData();
            $data['groupCatInfo'] = $groupCat->findAllData('','',1,1000);
            $data['project'] = $project::findAllData();
            return code::success($data);
        }
    }

    /*
     * 详情
     * @param Request $request
     * @author cpy(1442435897@qq.com)
     * @return Json
     */
    public function actionMovieDetail(){
        $model = new \backend\models\MovieCollege();
        $request =$this->request;
        $params = $request->bodyParams;
        $data = $model::findOneData($params['id']);
        return code::success($data);
    }

    /*
     * 修改
     * @param Request $request
     * @author cpy(1442435897@qq.com)
     * @return Json
     */
    public function actionSaveMovie()
    {
        $model = new \backend\models\MovieCollege();
        $session = Yii::$app->session;
        $request =$this->request;
        $params = $request->bodyParams;
        $id = $params['id'];
        unset($params['id']);
        if(!empty($params)) {
            if ($model->load($params, '')) {
                if ($model->validate()) {
                    $params['admin_id'] = $session->get('user_id');
                    $params['admin_name'] = $session->get('username');
                    if(empty($params['admin_id']) || empty($params['admin_name'])){
                        return code::error('error','请登录！');
                    }
                    $res = $model->saveInfo('update',$params,$id);
                    if(empty($res->errorInfo)){
                        return code::success('');
                    }else{
                        return code::error('error');
                    }
                }else{
                    $error = array_values($model->getErrors());
                    return code::error('',$error[0][0]);
                }
            }
        }else{
            $data = $model::findOneData($id);
            return code::success($data);
        }
    }

    /*
     * 删除
     * @param Request $request
     * @author cpy(1442435897@qq.com)
     * @return Json
     */
    public function actionDeleteMovie()
    {
        $model = new \backend\models\MovieCollege();
        $request =$this->request;
        $params = $request->bodyParams;
        $res = $model::deleteData($params['id']);
        if($res){
            return code::success($res);
        }else{
            return code::error('error');
        }
    }


}
